package exercise;

import java.io.File;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


public class Dom4j {
	
	public static void main(String[] args) {
		try {
			SAXReader reader =new SAXReader();
			Document document = reader.read(new File("sample.xml"));
			Element root = document.getRootElement();
			
			Iterator<Element> children =root.elementIterator();
			while(children.hasNext()){
				  Element el =children.next();
				  if(!"student".equals(el.getName())){
					  continue;
				  }
					System.out.println("--------student--------");
					System.out.println("id:"+el.attributeValue("id"));
					System.out.println("name:"+el.elementText("name"));
					System.out.println("gender:"+el.elementText("gender"));
					System.out.println("age:"+el.elementText("age"));
			}
			
			
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	

}
